import Flutter
import UIKit

public class SwiftFlutterPluginDemoPlugin: NSObject, FlutterPlugin {
    static var channel :FlutterMethodChannel!
  public static func register(with registrar: FlutterPluginRegistrar) {
     channel = FlutterMethodChannel(name: "flutter_plugin_demo", binaryMessenger: registrar.messenger())
    let instance = SwiftFlutterPluginDemoPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      
       // 1. 获取方法名和参数
      let method = call.method
      if (method == "getPlatformVersion"){
          result("iOS " + UIDevice.current.systemVersion)
      }else if (method == "test"){
          var dict = call.arguments as! [String: Any]
          dict["key3"] = "value3"
          result(dict)
          
          
             
          let time: TimeInterval = 2.0
          
          
          DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+time){
              
              let today = Date()
              var dict2 = ["today":self.date2String(today)]
              SwiftFlutterPluginDemoPlugin.channel.invokeMethod("flutter_plugin_demo_msg_from_native", arguments: dict2)
          }

          
      }
    
  }
    
    
    //日期 -> 字符串
    func date2String(_ date:Date, dateFormat:String = "yyyy-MM-dd HH:mm:ss") -> String {
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = dateFormat
        let date = formatter.string(from: date)
        return date
    }

}
